//
//  ViewController.m
//  CLImagePicker
//
//  Created by Criss on 2017/3/16.
//  Copyright © 2017年 Criss. All rights reserved.
//

#import "ViewController.h"
#import "OneViewController.h"
#import "CLImageShowView.h"
#import "CLImagePickerController.h"
#import "CLImageDisplayController.h"
#import "UIButton+Addition.h"
#import <Photos/Photos.h>

#import "Masonry.h"

@interface CLImageTableCell : UITableViewCell <CLImageDisplayControllerDelegate>

@property (weak, nonatomic) CLImageDisplayController *displayVC;
@property (copy, nonatomic) void(^reloadHeight)();
@property (copy, nonatomic) void(^pushClickBlock)();

@end

@implementation CLImageTableCell

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    CLImageDisplayController *displayVC = [CLImageDisplayController initWithImageUrls:@[@"http://pic.58pic.com/58pic/14/27/45/71r58PICmDM_1024.jpg", @"http://www.zhiyinlady.com/d/file/20170322/2e3da7aed9d6744388f5497651def758.jpg", @"http://wow.tgbus.com/uploadfiles_2396/201012/2010120115374742.jpg"/*, @"http://www.51modo.cc/upload/kindeditor/image/20150324/20150324155751_77766.jpg"*/]];
    displayVC.isShowImageByScrllView = YES;
    displayVC.displayDelegate = self;
    displayVC.imagesForRow = 4;
    displayVC.imageCounts = 14;
    self.displayVC = displayVC;
    [self.contentView addSubview:displayVC.view];
    
    [displayVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.contentView);
    }];
    
    UIButton *btn = [UIButton buttonWithNormalTitle:@"重新加载图片" titleColor:[UIColor redColor] superView:self.contentView addAction:^(UIButton * _Nullable sender) {
        if (self.pushClickBlock) {
            self.pushClickBlock();
        }
        
    }];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(displayVC.view.mas_bottom).mas_offset(15);
        make.centerX.bottom.equalTo(self.contentView);
    }];
}

- (void)cl_imageDispaly:(CLImageDisplayController *)imageDisplayController imageDisplayViewNewHeight:(CGFloat)height {
    
    [imageDisplayController.view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height).priorityHigh();
    }];
    if (self.reloadHeight) {
        self.reloadHeight();
    }
}

@end

@interface ViewController () <UITableViewDataSource>

@end

@implementation ViewController {
    UIView *_backView;
    CGFloat _height;
    NSArray *_titles;
}

- (void)showTableView {
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];
    [tableView registerClass:[CLImageTableCell class] forCellReuseIdentifier:@"imageViewcell"];
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.estimatedRowHeight = 100;
    tableView.dataSource = self;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    if (indexPath.row != 10) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
        NSString *des = @(indexPath.row).description;
        if (indexPath.row < _titles.count) {
            des = _titles[indexPath.row];
        }
        cell.textLabel.text = des;
    }else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"imageViewcell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        ((CLImageTableCell *)cell).reloadHeight = ^{
            [tableView reloadData];
        };
        __weak CLImageTableCell* weakCell = (CLImageTableCell *)cell;
        ((CLImageTableCell *)cell).pushClickBlock = ^{
            OneViewController *oneVC = [OneViewController new];
            oneVC.btnClickBlock = ^(NSArray<NSString *> *images, NSArray<NSString *> *titles) {
                _titles = titles;
                weakCell.displayVC.imageUrls = images;
                [tableView reloadData];
            };
            [self.navigationController pushViewController:oneVC animated:YES];
        };
    }
    return cell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showTableView];
}

@end

